package com.mallotec.android.timenote.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.leinardi.android.speeddial.SpeedDialView;
import com.mallotec.android.timenote.App;
import com.mallotec.android.timenote.R;
import com.mallotec.android.timenote.data.bean.Timenote;
import com.mallotec.android.timenote.databinding.ActivityTimenoteEdittingBinding;
import com.mallotec.android.timenote.ui.common.BaseActivity;
import com.mallotec.android.timenote.util.DateTimeUtil;
import com.mallotec.android.timenote.util.StringUtil;
import com.mallotec.android.timenote.viewmodel.CustomViewModelFactoryProviders;
import com.mallotec.android.timenote.viewmodel.TimenoteListViewModel;
import com.mallotec.android.timenote.viewmodel.factory.TimenoteListModelFactory;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

import java.text.ParseException;
import java.util.Calendar;

public class TimenoteEdittingActivity extends BaseActivity<ActivityTimenoteEdittingBinding> {

    private TimenoteListViewModel mViewModel;
    private TimenoteListModelFactory modelFactory;
    private Timenote timenote;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_timenote_editting;
    }

    @Override
    protected void onInitView(Bundle savedInstanceState) {
        // Set up ActionBar
        Toolbar toolbar = binding.toolbar;
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);  // 添加默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true);   // 设置返回键可用
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        modelFactory = CustomViewModelFactoryProviders.getTimenoteListViewModelFactory(this);
        mViewModel = new ViewModelProvider(this, modelFactory).get(TimenoteListViewModel.class);
        Intent intent = getIntent();
        int timenoteId = intent.getIntExtra("TimenoteId", -1);
        if (timenoteId != -1) {
            mViewModel.getTimenoteById(timenoteId).observe(this, timenote -> {
                this.timenote = timenote;
                binding.inputContent.setText(timenote.getTimenoteContent());
                binding.inputType.setText(timenote.getTimenoteType());
                binding.inputStartTime.setText(DateTimeUtil.calendarToString(timenote.getStartDatetime()));
                binding.inputPreEndTime.setText(DateTimeUtil.calendarToString(timenote.getPreEndDatetime()));
            });
        }
        binding.inputStartTime.setOnClickListener(view -> {
            DateTimeUtil.pickDateTime(this, binding.inputStartTime);
        });
        binding.inputPreEndTime.setOnClickListener(view -> {
            DateTimeUtil.pickDateTime(this, binding.inputPreEndTime);
        });
        binding.fabSpeedDialSave.setOnChangeListener(new SpeedDialView.OnChangeListener() {
            @Override
            public boolean onMainActionSelected() {
                if (saveTimenote(timenoteId)) {
                    finish();
                }
                return false;
            }

            @Override
            public void onToggleChanged(boolean isOpen) {

            }
        });
    }

    private boolean saveTimenote(int timenoteId) {
        String content = binding.inputContent.getText().toString();
        String type = binding.inputType.getText().toString();
        Calendar startDateTime = null;
        Calendar preEndDateTime = null;
        if (StringUtil.isEmpty(content)) {
            Snackbar.make(binding.fabSpeedDialSave, "请填写内容", Snackbar.LENGTH_SHORT).show();
            return false;
        }
        type = StringUtil.isEmpty(type) ? "默认" : type;
        try {
            startDateTime = DateTimeUtil.stringToCalendar(binding.inputStartTime.getText().toString());
            preEndDateTime = DateTimeUtil.stringToCalendar(binding.inputPreEndTime.getText().toString());
        } catch (ParseException e) {
            e.printStackTrace();
            Snackbar.make(binding.fabSpeedDialSave, "请选择时间", Snackbar.LENGTH_SHORT).show();
            return false;
        }
        if (timenoteId == -1) {
            Timenote timenote = new Timenote(
                    null,
                    startDateTime, preEndDateTime, null,
                    type,
                    content,
                    null, null, null
            );
            mViewModel.addTimenote(timenote);
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        } else {
            this.timenote.setTimenoteContent(content);
            this.timenote.setTimenoteType(type);
            this.timenote.setStartDatetime(startDateTime);
            this.timenote.setPreEndDatetime(preEndDateTime);
            mViewModel.updateTimenote(this.timenote);
            this.timenote = null;
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}